
#include "stdafx.h"
#include <allegro5/allegro_primitives.h>
#include "TankBody.h"

TankBody::TankBody(const char * modelName):
BaseComponent(modelName) {
  // Bouml preserved body begin 000216FB
  // Bouml preserved body end 000216FB
}

TankBody::~TankBody(){
  // Bouml preserved body begin 0002177B
  // Bouml preserved body end 0002177B
}

bool TankBody::AttachChasis(_TankChasis & chasis) {
  // Bouml preserved body begin 0001F67B

	_chasis = chasis;
	return true;
  // Bouml preserved body end 0001F67B
}

bool TankBody::AttachTurret(const _TankTurret & turret, char slot) {
  // Bouml preserved body begin 0001F6FB

	if ( slot >= _maxTurrets ) {
		return false;
	}

	_turrets[ slot] = turret;

	return true;
  // Bouml preserved body end 0001F6FB
}

void TankBody::Assemble() {
  // Bouml preserved body begin 0002187B
	if ( !_modelName.compare( "base1" ) ) {
        float hull[] = { -31.41, 80.30, 31.42, 80.30, 31.42, -37.41, -31.42, -37.41 };
        point turretCenter = { 0.00, 14.65 };
        
		LoadFigure( 4, hull );

		_turretSlots.clear();
		_turretSlots.insert( _turretSlots.begin() , turretCenter );

		_TankTurret emptyTurret;
		_turrets.clear();
		_turrets.resize( _maxTurrets = 1, emptyTurret );
	}
  // Bouml preserved body end 0002187B
}

//Draws object in given position

void TankBody::DrawAt(const BaseComponent::point & position) {
  // Bouml preserved body begin 0002507B
	ALLEGRO_TRANSFORM current, toUse;

	_chasis->DrawAt( position );
	BaseComponent::DrawAt( position );

	al_copy_transform( &current, al_get_current_transform() );
	al_copy_transform( &toUse, al_get_current_transform() );	
	al_rotate_transform( &toUse, _angle );
	al_translate_transform( &toUse, position.x, position.y );

	al_use_transform(&toUse);
	
	for ( int i = 0;  i < _turrets.size(); i++ ) {
		_turrets[ i]->DrawAt( _turretSlots[ i] );
	}

	al_use_transform( &current );
  // Bouml preserved body end 0002507B
}

//Rotates given turret into given angle.
void TankBody::SetTurretAngle(char slot, float angle) {
  // Bouml preserved body begin 000283FB
	if ( slot < _turrets.size() ) {
		_turrets[ slot]->SetAngle( angle );
	}
  // Bouml preserved body end 000283FB
}

